/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Interfaz;

import Gestores.GestorCanton;
import Gestores.GestorCedula;
import Gestores.GestorEstadoCivil;
import Gestores.GestorInstruccion;
import Gestores.GestorPersona;
import Gestores.GestorProfesion;
import Negocio.Canton;
import Negocio.Cedula;
import Negocio.EstadoCivil;
import Negocio.Instruccion;
import Negocio.Persona;
import Negocio.Profesion;
import java.util.ArrayList;
import java.util.Calendar;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;

/**
 *
 * @author Dvid
 */
public class VentanaCedula extends javax.swing.JInternalFrame {

    /**
     * Creates new form IngresoCedula
     */
    String fec;
    Cedula cedula = new Cedula();
    GestorCedula ced = new GestorCedula();
    Persona persona = new Persona();
    GestorPersona per = new GestorPersona();
    Canton canton = new Canton();
    GestorCanton cant = new GestorCanton();
    GestorEstadoCivil esta = new GestorEstadoCivil();
    EstadoCivil estado = new EstadoCivil();
    Instruccion instruccion = new Instruccion();
    GestorInstruccion instru = new GestorInstruccion();
    Profesion profesion = new Profesion();
    GestorProfesion prof = new GestorProfesion();
   
    public VentanaCedula() {
        initComponents();
        
        
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        lblfecha = new javax.swing.JLabel();
        txtfirma = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        btncrear = new javax.swing.JButton();
        txtcedula = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        cmcombo = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();
        cminstruccion = new javax.swing.JComboBox();
        jLabel6 = new javax.swing.JLabel();
        cmprofesion = new javax.swing.JComboBox();
        jLabel7 = new javax.swing.JLabel();
        cmcanton = new javax.swing.JComboBox();

        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameOpened(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        jLabel1.setText("Cedula");

        lblfecha.setText("fecha");

        jLabel3.setText("Firma");

        btncrear.setText("Crear");
        btncrear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btncrearActionPerformed(evt);
            }
        });

        jLabel2.setText("Numero de la persona");

        jLabel4.setText("Estado Civil");

        cmcombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jLabel5.setText("Instruccion");

        cminstruccion.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jLabel6.setText("Profesion");

        cmprofesion.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jLabel7.setText("Canton");

        cmcanton.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(lblfecha, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(19, 19, 19))
            .addGroup(layout.createSequentialGroup()
                .addGap(204, 204, 204)
                .addComponent(btncrear)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(59, 59, 59)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel7)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, 113, Short.MAX_VALUE)
                            .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel5)
                            .addComponent(jLabel6)
                            .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(58, 58, 58)
                                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(cmcanton, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addComponent(txtcedula)
                                        .addComponent(txtfirma, javax.swing.GroupLayout.DEFAULT_SIZE, 220, Short.MAX_VALUE)
                                        .addComponent(cmcombo, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(cminstruccion, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(cmprofesion, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE)))))))
                .addGap(0, 120, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(14, 14, 14)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblfecha, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(12, 12, 12)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtcedula, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtfirma, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addComponent(cmcombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5)
                    .addComponent(cminstruccion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel6)
                    .addComponent(cmprofesion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel7)
                    .addComponent(cmcanton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(22, 22, 22)
                .addComponent(btncrear)
                .addContainerGap(47, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btncrearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btncrearActionPerformed
        persona = per.buscar(txtcedula.getText());
        if (persona!=null){
         canton = cant.buscar((String)cmcanton.getSelectedItem());
         estado = esta.buscar((Integer)cmcombo.getSelectedItem());
         profesion = prof.buscar((Integer)cmprofesion.getSelectedItem());
         instruccion = instru.buscar((Integer)cminstruccion.getSelectedItem());
         cedula.setCanton(canton);
         cedula.setEstado(estado);
         cedula.setFecha(fec);
         cedula.setInstruccion(instruccion);
         cedula.setPersona(persona);
         cedula.setProfesion(profesion);
         ced.grabar(cedula);
         JOptionPane.showMessageDialog(null, "Cedula Almacenada ");
         txtcedula.setText("");
         txtfirma.setText("");
     }
        else{
            JOptionPane.showMessageDialog(null, "No existe la persona ");
        }
   
    }//GEN-LAST:event_btncrearActionPerformed

    private void formInternalFrameOpened(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameOpened
       Calendar Cal= Calendar.getInstance();
        fec= Cal.get(Cal.DATE)+"/"+(Cal.get(Cal.MONTH)+1)+"/"+Cal.get(Cal.YEAR);
        lblfecha.setText(fec);
        instru.listar();
        prof.listar();
        cant.listar();
        esta.listar();
        cmcanton.setModel(new javax.swing.DefaultComboBoxModel(generarCantones(cant.getCantones())));
        cmcombo.setModel(new javax.swing.DefaultComboBoxModel(generarEstados(esta.getEstados())));
        cmprofesion.setModel(new javax.swing.DefaultComboBoxModel(generarProfesiones(prof.getProfesiones())));
        cminstruccion.setModel(new javax.swing.DefaultComboBoxModel(generarInstrucciones(instru.getInstrucciones())));
                
        
    }//GEN-LAST:event_formInternalFrameOpened

   
    
    private String[] generarCantones(ArrayList list){
        String[] lista = new String[list.size()];
        Canton aux;
        for (int i = 0 ; i <list.size(); i++){
            aux = (Canton)list.get(i);
            lista[i] = aux.getId();
        }
        return lista;
    }
    
    private String[] generarInstrucciones(ArrayList list){
        String[] lista = new String[list.size()];
        Instruccion aux;
        for (int i = 0 ; i <list.size(); i++){
            aux = (Instruccion)list.get(i);
            lista[i] =String.valueOf(aux.getId());
        }
        return lista;
    }
    
    private String[] generarEstados(ArrayList list){
        String[] lista = new String[list.size()];
        EstadoCivil aux;
        for (int i = 0 ; i <list.size(); i++){
            aux = (EstadoCivil)list.get(i);
            lista[i] = String.valueOf(aux.getId());
        }
        return lista;
    }
    
     private String[] generarProfesiones(ArrayList list){
        String[] lista = new String[list.size()];
        Profesion aux;
        for (int i = 0 ; i <list.size(); i++){
            aux = (Profesion)list.get(i);
            lista[i] = String.valueOf(aux.getId());
        }
        return lista;
    }
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btncrear;
    private javax.swing.JComboBox cmcanton;
    private javax.swing.JComboBox cmcombo;
    private javax.swing.JComboBox cminstruccion;
    private javax.swing.JComboBox cmprofesion;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel lblfecha;
    private javax.swing.JTextField txtcedula;
    private javax.swing.JTextField txtfirma;
    // End of variables declaration//GEN-END:variables
}
